package com.test.testCode.exportExcel;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;
import java.util.*;

public class ExportExcel {

    public static void main(String[] args) throws Exception {
        String[] headers = {"头部一", "头部二", "头部三", "头部四", "头部五", "头部六", "头部七"};

        List<Map> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            Map<String, Integer> map = new LinkedHashMap<>();
            map.put("头部一", i + 1);
            map.put("头部二", i + 2);
            map.put("头部三", i + 3);
            map.put("头部四", i + 4);
            map.put("头部五", i + 5);
            map.put("头部六", i + 6);
            map.put("头部七", i + 7);
            list.add(map);
        }
        exportExcel("test.xls", "测试文件", headers, list, "yyyyMMdd");
    }

    public static void exportExcel(String fileName, String title, String[] headers, List<Map> dataset, String pattern) throws Exception {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet(title);
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        // 列名
        Row row = sheet.createRow(0);

        String[] h = null;
        if (CollectionUtils.isNotEmpty(dataset)) {
            Map headerMap = dataset.iterator().next();
            Set<String> mapKey1 = (Set<String>) headerMap.keySet();
            h = mapKey1.toArray(new String[]{});
            System.out.println(StringUtils.join(h, ","));
            //将数据反序排列
            ArrayUtils.reverse(h);
            System.out.println(StringUtils.join(h, ","));
        }


        for (int i = 0; i < h.length; i++) {
            Cell cell = row.createCell(i);
            sheet.setColumnWidth(i, 3000);
            cell.setCellStyle(style);
            cell.setCellValue(h[i]);
        }
        int index = 0;
        for (Map mappp : dataset) {
            index++;
            ListIterator<Map.Entry> lsits = new ArrayList<Map.Entry>(mappp.entrySet()).listIterator(mappp.size());
            int num = 0;
            row = sheet.createRow(index);
            while (lsits.hasPrevious()) {
                Cell cell = row.createCell(num);
                num++;
                Map.Entry entry = lsits.previous();
                cell.setCellStyle(style);
                Object obj = entry.getValue();
                if (obj instanceof Integer) {
                    cell.setCellValue((Integer) obj);
                } else if (obj instanceof Double) {
                    cell.setCellValue((Double) obj);
                } else if (obj instanceof Float) {
                    cell.setCellValue((Float) obj);
                } else {
                    cell.setCellValue((String) obj);
                }
            }
        }
        FileOutputStream fos;
        fos = new FileOutputStream(fileName);
        workbook.write(fos);
        fos.close();
    }
}
